package cn.highedu.coolsharkhub.controller;

import cn.highedu.coolsharkhub.service.exception.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 统一处理控制器异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理控制器类中捕获的EntityNotFoundException异常
     * @param e EntityNotFoundException异常
     * @return 异常信息
     */
    @ExceptionHandler(EntityNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    public String handlerEntityNotFoundException(EntityNotFoundException e) {
        return e.getMessage();
    }

    @ExceptionHandler(BlankParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST) // 400
    public String handlerBlankParameterException(BlankParameterException e) {
        return e.getMessage();
    }

    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    public String handlerUserNotFoundException(UserNotFoundException e) {
        return e.getMessage();
    }

    /**
     * 处理文件上传异常(文件格式不合法)
     * @param e FileSuffixNotAllowedException异常
     * @return 异常信息
     */
    @ExceptionHandler(FileSuffixNotAllowedException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST) // 400
    public String handlerFileSuffixNotAllowedException(FileSuffixNotAllowedException e) {
        return e.getMessage();
    }

    /**
     * 处理保存文件失败异常
     * @param e FileSaveException异常
     * @return 异常信息
     */
    @ExceptionHandler(FileSaveException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //500
    public String handlerFileSaveException(FileSaveException e) {
        return e.getMessage();
    }
}
